package com.it.gmall.product.utils;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class FileUploadUtils {

    public static TrackerClient trackerClient = null;

    static {
        try {
            // 加载配置文件
            String path = ClassUtils.getDefaultClassLoader().getResource("fdfs_client.conf").getPath();
            ClientGlobal.init(path);
            // 1.连接调度器 tracker
            trackerClient = new TrackerClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 上传文件
    public static String uploadFile(byte[] fileBytes,String extension) throws Exception{

        // 2.根据调度器获取存储节点
        TrackerServer connection = trackerClient.getConnection();
        // 3.获取存储节点
        StorageClient1 client1 = new StorageClient1(connection,null);
        // 4.上传文件
        String filePath = client1.upload_file1(
                fileBytes,
                extension,
                null
        );
        return filePath;
    }

    // 下载文件
}
